﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Collections.Generic;

public class GameInfo : MonoBehaviour {

    public static GameInfo _instance;
    private string _path = @"./GameInfo";
    //成绩字典
    public Dictionary<string, int> _scoreDic = new Dictionary<string, int>();
    void Awake()
    {
        _instance = this;
    }
	// Use this for initialization
	void Start () {
        GetScore(); 
        GetRankScore();
	}

    private void GetScore()
    {
        using (FileStream fsReader = new FileStream(_path, FileMode.OpenOrCreate, FileAccess.Read))
        {
            byte[] buffer = new byte[1024 * 1024 * 1];
            int r = fsReader.Read(buffer, 0, buffer.Length);
            string[] gameInfo = Encoding.Default.GetString(buffer, 0, r).Trim().Split('\n');
            foreach (string item in gameInfo)
            {
                if (item.Trim() != "")
                {
                    string[] info = item.Trim().Split('|');
                    SetScore(info[0], int.Parse(info[1]));
                }
            }

        }
        if (PlayerPrefs.HasKey("newScore"))
        {
            string name = PlayerPrefs.HasKey("name") ? PlayerPrefs.GetString("name") : "未知";
            int score = PlayerPrefs.GetInt("newScore");
            PlayerPrefs.DeleteAll();
            SetScore(name, score);
        }
    }

    public string GetRankToString()
    {
        string str = "";

        foreach (KeyValuePair<string, int> kv in _scoreDic)
        {
            str += kv.Key + ":" + kv.Value + "\n";
        }

        return str;
    }

    private void SetScore(string name,int score)
    {
        if (_scoreDic.ContainsKey(name))
        {
            int i = 1;
            do
            {
                name = name + "("+i+")";
            } while (_scoreDic.ContainsKey(name));
        }
        _scoreDic.Add(name,score);
    }

    public void GetRankScore()
    {
        List<int> scorelist = new List<int>();
        List<string> namelist = new List<string>();
        Dictionary<string, int> tempDic = new Dictionary<string, int>();
        foreach (KeyValuePair<string,int> kv in _scoreDic)
        {
            scorelist.Add(kv.Value);
        }
        scorelist.Sort();
        scorelist.Reverse();
        for (int i = 0; i < 6; i++)
        {
            if (i<scorelist.Count)
            {
                foreach (KeyValuePair<string, int> kv in _scoreDic)
                {
                    if (kv.Value == scorelist[i] && !tempDic.ContainsKey(kv.Key))
                    {
                        tempDic.Add(kv.Key,kv.Value);
                    }
                }
            }
            else
            {
                break;
            }
        }
        _scoreDic.Clear();
        _scoreDic = tempDic;

    }

    public int GetBestScore()
    {
        int bestScore = 0;
        foreach (KeyValuePair<string,int> kv in _scoreDic)
        {
            if (kv.Value>bestScore)
            {
                bestScore = kv.Value;
            }
        }
        return bestScore;
    }
	
	// Update is called once per frame
	void Update () {
	
	}

    internal void SaveInfo()
    {
        using (FileStream fsWriter = new FileStream(_path, FileMode.Create, FileAccess.Write))
        {
            string GameInfo = "";
            foreach (KeyValuePair<string,int> kv in _scoreDic)
            {
                GameInfo += kv.Key + "|" + kv.Value + "\n";
            }
            byte[] buffer = Encoding.Default.GetBytes(GameInfo);
            fsWriter.Write(buffer, 0, buffer.Length);

        }
    }
}
